package com.example.demo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.Common.Result;
import com.example.demo.entity.FormBusiness;
import com.example.demo.mapper.FormBusinessMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 业务表单数据表服务控制器
 *
 * @author kancy
 * @since 2021-11-22 20:10:33
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/formBusiness")
public class FormBusinessController {
    @Resource
    private final FormBusinessMapper formBusinessMapper;


    @PostMapping
    public Result<?> save(@RequestBody FormBusiness formBusiness){
        formBusinessMapper.insert(formBusiness);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody FormBusiness formBusiness){
        formBusinessMapper.updateById(formBusiness);
        return Result.success();
    }

    /*
    目前是采用直接删除记录，如果需要留痕，只需要修改删除标记isdel=1则可
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        formBusinessMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping
    public Result<?> find(@RequestParam(defaultValue = "") String search,
                          @RequestParam(defaultValue = "1") Long templateid){

        LambdaQueryWrapper<FormBusiness> wrapper = Wrappers.<FormBusiness>lambdaQuery() ;

        if(StringUtils.isNotEmpty(search)){
            wrapper.like(FormBusiness::getPersonName, search);
        }

        //过滤表单模板关联
        wrapper.eq(FormBusiness::getTemplateid, templateid);
        //过滤删除标记
        wrapper.eq(FormBusiness::getIsdel, "0");

        List<FormBusiness> formBusinesses = formBusinessMapper.selectList(wrapper);

        return Result.success(formBusinesses);
    }
}